package com.asiainfo.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.asiainfo.bean.BillBean;

import com.asiainfo.bean.MealBean;


import com.asiainfo.bean.UserSessionBean;
import com.asiainfo.dao.BaseDao;
import com.asiainfo.util.SessionInfoUtil;


@Controller
@RequestMapping("/myBillController")
public class MyBillController extends ModelAndView{
	
	private Logger logger = LogManager.getLogger(this.getClass());
	
	
	/**
	 * 数据操作封装DAO
	 */
	@Autowired
	private BaseDao dao;

	/**
	 * 方法描述：跳转到当月消费情况页面
	 * 
	 * return 当月消费情况页面Model
	 */
	@RequestMapping("/monthBillView.do")
	public ModelAndView monthBillView() {
		this.setViewName("helper/monthBill");
		
		return this;	
		}

	/**
	 * 方法描述：跳转到我的账单页面
	 * 
	 * return 我的账单页面Model
	 */
	@RequestMapping("/myBillView.do")
	public ModelAndView myBillView() {
		this.setViewName("helper/myBill");
		this.addObject("data", "none");
		this.addObject("nodata", "none");
		return this;	
		}

	/**
	 * 方法描述：跳转到我的账单页面
	 * 
	 * return 我的账单页面Model
	 */
	@RequestMapping("/myBillData.do")
	public ModelAndView myBillData() {
		this.setViewName("helper/myBill");
		
		return this;	
		}

	/**
	 * 方法描述：跳转到账单分析页面
	 * 
	 * return 账单分析页面Model
	 */
	@RequestMapping("/billAnalysisView.do")
	public ModelAndView billAnalysisView() {
		this.setViewName("helper/billAnalysis");
		
		return this;	
		}

	
	/**
	 * 方法描述：查看当月消费操作
	 * 
	 */
	@RequestMapping(value = "/monthBill.do")
	 
	public @ResponseBody
	JSONObject monthBill(@RequestBody BillBean billBean, 
			HttpServletRequest request) throws Exception {
		JSONObject retMsg = new JSONObject();
		
        UserSessionBean userSessionBean = SessionInfoUtil.getUserSessBean(request.getSession());
		String userPhone = userSessionBean.getPhone();
		
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");//设置日期格式
		String currentDate = df.format(new Date());// new Date()为获取当前系统时间
		
		if(userPhone !=null){
			billBean.setMonth(currentDate);
			
			//判断该账单表是否有该号码
			int isMealUseCost = (Integer)this.dao.getDAO().selectOne(
					"BillMapper.isMealUseCost", billBean);
			if (isMealUseCost==0) {
				this.addObject("billTotal", "0");
	 			this.addObject("callCost", "0");
	 			this.addObject("dataCost", "0");
	 			this.addObject("messageCost", "0");
	 			this.addObject("otherCost", "0");
			}else {
			BillBean billCost = dao.getDAO().selectOne(
 					"BillMapper.querybillCost", billBean);
			
			this.addObject("billTotal", billCost.getBilltotal());
 			this.addObject("callCost", billCost.getCall_cost());
 			this.addObject("dataCost", billCost.getData_cost());
 			this.addObject("messageCost", billCost.getMessage_cost());
 			this.addObject("otherCost", billCost.getOther_business_cost());
			}
			
			BillBean balance = dao.getDAO().selectOne(
 					"BillMapper.queryBalance", billBean.getPhone());
			this.addObject("currentBalance", balance.getReal_time_balance());
			
			retMsg.put("flag", true);
		}else{
			retMsg.put("flag", false);
		}
		return retMsg;
	}
	
	
	/**
	 * 方法描述：查看我的账单操作
	 * 
	 */
	@RequestMapping(value = "/myBill.do")
	 
	public @ResponseBody
	JSONObject myBill(@RequestBody BillBean billBean, 
			HttpServletRequest request) throws Exception {
		JSONObject retMsg = new JSONObject();
		
        UserSessionBean userSessionBean = SessionInfoUtil.getUserSessBean(request.getSession());
		String userPhone = userSessionBean.getPhone();
		
		
		if(userPhone !=null){
			
			
			//判断该账单表是否有该号码
			int isMealUseCost = (Integer)this.dao.getDAO().selectOne(
					"BillMapper.isMealUseCost", billBean);
			if (isMealUseCost==0) {
				retMsg.put("NoData", true);//无数据
				
				this.addObject("mybillTotal", "0");
	 			this.addObject("mycallCost", "0");
	 			this.addObject("mydataCost","0");
	 			this.addObject("mymessageCost", "0");
	 			this.addObject("myotherCost", "0");
	 			this.addObject("data", "none");
	 			this.addObject("nodata", "block");
			}else {
			BillBean billCost = dao.getDAO().selectOne(
 					"BillMapper.querybillCost", billBean);
			
			this.addObject("mybillTotal", billCost.getBilltotal());
 			this.addObject("mycallCost", billCost.getCall_cost());
 			this.addObject("mydataCost", billCost.getData_cost());
 			this.addObject("mymessageCost", billCost.getMessage_cost());
 			this.addObject("myotherCost", billCost.getOther_business_cost());
 			this.addObject("data", "block");
 			this.addObject("nodata", "none");
 			retMsg.put("flag", true);
			}
			
		}else{
			retMsg.put("flag", false);
		}
		return retMsg;
	}
	
	
	
	/**
	 * 方法描述：查看账单分析操作
	 * 
	 */
	@RequestMapping(value = "/billAnalysis.do")
	 
	public @ResponseBody
	JSONObject billAnalysis(@RequestBody BillBean billBean, 
			HttpServletRequest request) throws Exception {
		JSONObject retMsg = new JSONObject();
		
        UserSessionBean userSessionBean = SessionInfoUtil.getUserSessBean(request.getSession());
		String userPhone = userSessionBean.getPhone();
		
		
		if(userPhone !=null){
			
			//判断该账单表是否有该号码
			int isCost = (Integer)this.dao.getDAO().selectOne(
					"BillMapper.isCost", billBean);
			this.addObject("isCost", isCost);
			if (isCost!=0) {
				
				List<BillBean> billCostList = dao.getDAO().selectList(
 					"BillMapper.querybillCostList", billBean);
			
			this.addObject("billCostList", billCostList);
 			
			}
			
			//判断是否有使用量（当月之前）
			int isUse = (Integer)this.dao.getDAO().selectOne(
					"BillMapper.isUse", billBean);
			if(isUse==0){
				this.addObject("Avg_call_use","0");
				this.addObject("Avg_data_use","0");
				this.addObject("Avg_message_use","0");
				billBean.setAvg_call_use("0");
				billBean.setAvg_data_use("0");
				billBean.setAvg_message_use("0");
			}else{
				BillBean avgUse = dao.getDAO().selectOne(
	 					"BillMapper.queryAvgUse", billBean);
				
				this.addObject("Avg_call_use",avgUse.getAvg_call_use());
				this.addObject("Avg_data_use",avgUse.getAvg_data_use());
				this.addObject("Avg_message_use",avgUse.getAvg_message_use());
				billBean.setAvg_call_use(avgUse.getAvg_call_use());
				billBean.setAvg_data_use(avgUse.getAvg_data_use());
				billBean.setAvg_message_use(avgUse.getAvg_message_use());
			}
			
			for(int i=0;i<=1000;i++){
				billBean.setI(String.valueOf(i));
				int isRecommendMeal = (Integer)this.dao.getDAO().selectOne(
						"BillMapper.isRecommendMeal", billBean);
				
				if(isRecommendMeal>0){
					List<MealBean> recommendMealList = dao.getDAO().selectList(
		 					"BillMapper.queryRecommendMeal", billBean);
					
					this.addObject("recommendMealList",recommendMealList);
					
					break;
				}
				
			}
			
			retMsg.put("flag", true);
		}else{
			retMsg.put("flag", false);
		}
		return retMsg;
	}
}
